package sever

import (
	"fmt"
	keep "keep/common/proto"
	"keep/rpc/model"
)

func KeepReservationInfo(in *keep.KeepReservationInfoReq) (*keep.KeepReservationInfoResp, error) {
	g := model.GroupClass{}

	info, err := g.FindKeepReservationInfo(in.Name)
	if err != nil {
		return nil, fmt.Errorf("商品详情查询失败")
	}

	var all []*keep.KeepReservationList

	for _, i := range info {
		all = append(all, &keep.KeepReservationList{
			Name:    i.Name,
			Price:   float32(i.Price),
			Comment: i.Comment,
			Date:    i.Dates,
			Tine:    i.Time,
			Stadium: i.Stadium,
			Address: i.Address,
			Num:     i.Num,
			Coach:   i.Coach,
		})
	}

	return &keep.KeepReservationInfoResp{
		List: all,
	}, nil

}
func KeepReservation(in *keep.KeepReservationReq) (*keep.KeepReservationResp, error) {
	g := model.GroupClass{}
	r := model.Reservation{}
	class, err := g.FindClassId(in.ClassId)
	if err != nil {
		return nil, fmt.Errorf("没有该课程")
	}
	_, err = r.CreateKeepReservation(in, class.Price)
	if err != nil {
		return nil, fmt.Errorf("团课预约失败")
	}
	return &keep.KeepReservationResp{
		Success: true,
	}, nil

}
func ReservationConfirm(in *keep.ReservationConfirmReq) (*keep.ReservationConfirmResp, error) {
	g := model.GroupClass{}
	r := model.Reservation{}

	class, err := g.FindClassId(in.ClassId)
	if err != nil {
		return nil, fmt.Errorf("没有该课程")
	}
	_, err = r.CreateReservation(in, class.Price)
	if err != nil {
		return nil, fmt.Errorf("团课预约失败")
	}

	info, err := g.FindKeepReservation(in.ClassId)
	if err != nil {
		return nil, fmt.Errorf("团课详情查询失败")
	}

	var all []*keep.Reservation

	for _, i := range info {
		all = append(all, &keep.Reservation{
			Name:    i.Name,
			Date:    i.Dates,
			Tine:    i.Time,
			Stadium: i.Stadium,
			Address: i.Address,
			Num:     i.Num,
			Coach:   i.Coach,
		})
	}

	return &keep.ReservationConfirmResp{
		List: all,
	}, nil

}
func MyReservation(in *keep.MyReservationInfoReq) (*keep.MyReservationInfoResp, error) {
	g := model.GroupClass{}
	r := model.Reservation{}
	class, err := g.FindClass()
	if err != nil {
		return nil, fmt.Errorf("没有该课程")
	}

	my, err := r.FindReservation()
	if err != nil {
		return nil, fmt.Errorf("没有该预约")
	}
	var all []*keep.MyReservationList

	for _, i := range class {
		all = append(all, &keep.MyReservationList{
			Name:    i.Name,
			Date:    i.Dates,
			Tine:    i.Time,
			Stadium: i.Stadium,
			Address: i.Address,
			Coach:   i.Coach,
		})
	}
	for _, i := range my {
		all = append(all, &keep.MyReservationList{
			Sum:    i.Sum,
			Notes:  i.Notes,
			Status: int64(i.Status),
		})
	}
	return &keep.MyReservationInfoResp{
		List: all,
	}, nil
}
func RemindUser(in *keep.RemindUserReq) (*keep.RemindUserResp, error) {

	if in.Time > 30 {
		return nil, fmt.Errorf("时间大于30分钟")
	}
	return &keep.RemindUserResp{Success: true}, nil
}
func DelReservation(in *keep.DelReservationReq) (*keep.DelReservationResp, error) {
	if in.Time < 60 {
		return nil, fmt.Errorf("时间小于60分钟")
	}
	r := model.Reservation{}
	_, err := r.DelReservations(in.Id)
	if err != nil {
		return nil, fmt.Errorf("取消预约失败")
	}

	return &keep.DelReservationResp{Success: true}, nil
}
